<template>
  <div style="display: flex; margin-bottom: 20px">
    <el-input
      style="width: 300px"
      placeholder="输入用户名"
      v-model="params.searchKey"
      clearable
    ></el-input>
    <el-button type="primary" style="margin-left: 10px" @click="getList"
      >搜索</el-button
    >
  </div>
  <el-table :data="userData" style="width: 100%">
    <el-table-column prop="id" label="ID" />
    <el-table-column prop="avatar" label="头像">
      <template #default="{ row }">
        <img :src="row.avatar" alt="" style="width: 50px; height: 50px" />
      </template>
    </el-table-column>
    <el-table-column prop="name" label="用户名" />
    <el-table-column prop="email" label="邮箱" />
    <el-table-column prop="status" label="状态">
      <template #default="{ row }">
        <el-tag type="success" v-if="row.status == Enums.USER_ENUMS.正常"
          >正常</el-tag
        >
        <el-tag type="danger" v-if="row.status == Enums.USER_ENUMS.禁用"
          >禁用</el-tag
        >
      </template>
    </el-table-column>
    <el-table-column prop="createAt" label="注册时间" />
    <el-table-column prop="action" label="操作">
      <template #default="{ row }">
        <el-button
          type="danger"
          size="small"
          v-if="row.status == Enums.USER_ENUMS.正常"
          @click="editStatus(row.id)"
          >封禁</el-button
        >
        <el-button
          type="warning"
          size="small"
          v-if="row.status == Enums.USER_ENUMS.禁用"
          @click="editStatus(row.id)"
          >解除禁用</el-button
        >
      </template>
    </el-table-column>
  </el-table>
  <!-- 分页 -->
  <el-pagination
    style="margin-top: 20px"
    background
    layout="total, prev, pager, next"
    :total="total"
    :page-size="params.pageSize"
    @current-change="change"
  ></el-pagination>
</template>
<script setup>
import { ref } from "vue";
import Enums from "@/utils/Enums";
import { banUser, getUserList } from "@/api/admin";
import { ElMessage } from "element-plus";
const userData = ref([]);
const params = ref({
  page: 1,
  pageSize: 10,
  searchKey: "",
});
const total = ref(0);
const getList = async () => {
  getUserList(params.value)
    .then((res) => {
      userData.value = res.data.data.records;
      total.value = res.data.data.total;
    })
    .catch((err) => {
      console.error(err);
    });
};
const change = (page) => {
  params.value.page = page;
  getList();
};
const editStatus = async (id) => {
  await banUser(id);
  ElMessage.success("操作成功！");
  getList();
};
getList();
</script>
